package com.banciyuan.bcywebview.utils.universalimageloader.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.file.ImageManager;
import com.banciyuan.bcywebview.utils.universalimageloader.cache.disc.impl.ext.LruDiscCache;
import com.banciyuan.bcywebview.utils.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.banciyuan.bcywebview.utils.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.banciyuan.bcywebview.utils.universalimageloader.core.DisplayImageOptions;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoaderConfiguration;
import com.banciyuan.bcywebview.utils.universalimageloader.core.assist.ImageScaleType;
import com.banciyuan.bcywebview.utils.universalimageloader.core.assist.LoadedFrom;
import com.banciyuan.bcywebview.utils.universalimageloader.core.assist.QueueProcessingType;
import com.banciyuan.bcywebview.utils.universalimageloader.core.display.BitmapDisplayer;
import com.banciyuan.bcywebview.utils.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.banciyuan.bcywebview.utils.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.banciyuan.bcywebview.utils.universalimageloader.core.imageaware.ImageAware;

import java.io.File;
import java.io.IOException;

/**
 * Created by squall1989 on 2016/12/30.
 */

public class
ImageOptions {

    public static DisplayImageOptions options;
    public static DisplayImageOptions optionsComment;
    public static DisplayImageOptions optionsCopy;
    public static DisplayImageOptions options_none;
    public static DisplayImageOptions optionHead;
    public static DisplayImageOptions optionsTimeline;
    public static DisplayImageOptions optionsDetail;
    public static DisplayImageOptions optionsGray;
    public static DisplayImageOptions options_noneDisplay;
    public static DisplayImageOptions options_half;
    public static DisplayImageOptions optionsSimpleDispayer;
    public static DisplayImageOptions optionsSpotPic;

    public static void init(Context mContext) {
        //这里指定了缓存文件的存放地址
        File mFile = new File(ImageManager.getImageFile(mContext));
        if (!mFile.exists()) mFile.mkdirs();

        LruDiscCache mLruCache = null;
        try {
            mLruCache = new LruDiscCache(mFile, new Md5FileNameGenerator(), 360 * 1024 * 1024);
        } catch (IOException e) {
        }

        Runtime info = Runtime.getRuntime();
        long memory = info.maxMemory();
        //具体设置请参考android-UIL的文档
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
                .diskCache(mLruCache)
                .memoryCache(new UsingFreqLimitedMemoryCache((int) (memory / 10)))
                .denyCacheImageMultipleSizesInMemory()
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                .build();
        ImageLoader.getInstance().init(config);

        BitmapDisplayer displayer = new FadeInBitmapDisplayer(250) {
            @Override
            public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
                if (loadedFrom != LoadedFrom.MEMORY_CACHE) {
                    super.display(bitmap, imageAware, loadedFrom);
                } else {
                    imageAware.setImageBitmap(bitmap);
                }
            }
        };

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loading)
                .showImageForEmptyUri(R.drawable.loading_error)
                .showImageOnFail(R.drawable.loading_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(displayer).build();

        optionsComment = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.comment_holder)
                .showImageForEmptyUri(R.drawable.loading_error)
                .showImageOnFail(R.drawable.loading_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(displayer).build();

        options_none = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(displayer).build();

        options_noneDisplay = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

        optionsTimeline = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loading)
                .showImageForEmptyUri(R.drawable.loading_error)
                .showImageOnFail(R.drawable.loading)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .displayer(displayer).build();

        optionsCopy = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .considerExifParams(false)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .cacheOnDisk(true)
                .cacheInMemory(false)
                .build();

        options_half = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .considerExifParams(false)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .displayer(displayer).build();

        optionHead = new DisplayImageOptions.Builder().resetViewBeforeLoading(true).considerExifParams(false)
                .showImageOnLoading(R.drawable.user_pic_big)
                .showImageForEmptyUri(R.drawable.user_pic_big)
                .showImageOnFail(R.drawable.user_pic_big)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

        optionsDetail = new DisplayImageOptions.Builder().resetViewBeforeLoading(true).considerExifParams(false)
                .showImageOnLoading(R.drawable.loading)
                .showImageForEmptyUri(R.drawable.loading_error)
                .showImageOnFail(R.drawable.loading_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.NONE)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .displayer(displayer).build();

        optionsGray = new DisplayImageOptions.Builder().resetViewBeforeLoading(true).considerExifParams(false)
                .showImageOnLoading(R.color.goods_back)
                .showImageForEmptyUri(R.color.goods_back)
                .showImageOnFail(R.color.goods_back)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(displayer).build();

        options_noneDisplay = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loading)
                .showImageForEmptyUri(R.drawable.loading_error)
                .showImageOnFail(R.drawable.loading_error)
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

        optionsSimpleDispayer = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loading)
                .showImageForEmptyUri(R.drawable.loading_error)
                .showImageOnFail(R.drawable.loading_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new SimpleBitmapDisplayer()).build();

        optionsSpotPic = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.spot_default_pic)
                .showImageForEmptyUri(R.drawable.spot_default_pic)
                .showImageOnFail(R.drawable.spot_default_pic)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new SimpleBitmapDisplayer()).build();
    }


    private ImageOptions() {

    }


}
